<template>
  <div class="graduate page">
    <!-- 顶部搜索区域 -->
    <div class="search-header">
      <!-- 搜索类型选择 -->
      <div class="type-selector">
        <div 
          v-for="option in searchTypeOptions" 
          :key="option.value"
          :class="['type-item', { active: searchType === option.value }]"
          @click="searchType = option.value"
        >
          {{ option.text }}
        </div>
      </div>
      
      <!-- 名称搜索框,仅在searchType=5时显示 -->
      <div class="search-box" v-show="searchType === 5">
        <el-input
          v-model="searchKey"
          placeholder="请输入校友姓名"
          class="search-input"
          @keyup.enter.native="handleSearch"
        >
          <el-button 
            slot="append" 
            icon="el-icon-search"
            @click="handleSearch"
          ></el-button>
        </el-input>
      </div>
    </div>

    <!-- 校友列表 -->
    <div class="alumni-list">
      <el-card 
        v-for="item in alumniList" 
        :key="item.id"
        class="alumni-item"
        shadow="hover"
      >
        <div class="item-content">
          <div class="main-info" @click="goToDetail(item)">
            <div class="avatar">
              <el-avatar 
                :size="50"
                :src="getAvatarSrc(item.profilePicture)"
                class="alumni-avatar"
              ></el-avatar>
            </div>
            <div class="info">
              <div class="name">{{ item.name }}</div>
              <div class="detail">
                {{ item.college }} · {{ item.major }} · {{ item.admissionYear }}级
              </div>
            </div>
            <i class="el-icon-arrow-right arrow-icon"></i>
          </div>
          <!-- 移除 el-tooltip，直接使用按钮 -->
          <el-button 
            type="primary"
            size="small"
            icon="el-icon-message"
            circle
            class="message-btn"
            @click="handleSendMessage(item)"
          ></el-button>
        </div>
      </el-card>

      <!-- 加载更多 -->
      <div class="load-more" v-if="alumniList.length > 0">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          @current-change="handlePageChange"
        >
        </el-pagination>
      </div>

      <!-- 空状态 -->
      <el-empty
        v-if="alumniList.length === 0"
        description="暂无数据"
      ></el-empty>

      <!-- 加载状态 -->
      <div class="loading-state" v-if="loading">
      </div>

      <!-- 私信弹窗 -->
      <el-dialog
        title="发送私信"
        :visible.sync="messageDialogVisible"
        width="90%"
        custom-class="message-dialog"
      >
        <el-form :model="messageForm" ref="messageForm" :rules="messageRules">
          <el-form-item prop="content">
            <el-input
              type="textarea"
              v-model="messageForm.content"
              :rows="4"
              placeholder="请输入私信内容..."
              :maxlength="200"
              show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="messageDialogVisible = false">取 消</el-button>
          <el-button 
            type="primary" 
            @click="sendMessage" 
            :loading="sending"
          >发 送</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAlumniList, sendMessage } from "@/api/api";

export default {
  name: "Graduate",
  data() {
    return {
      name:'',
      searchKey: '',
      searchType: 5,
      searchTypeOptions: [
        { text: '同市', value: 1 },
        { text: '同班', value: 3 },
        { text: '学院', value: 4 },
        { text: '名称', value: 5 }
      ],
      activeTab: 'all',
      alumniList: [],
      loading: false,
      total: 0,
      currentPage: 1,
      pageSize: 10,
      loadingText: '加载中...',
      messageDialogVisible: false,
      sending: false,
      currentAlumni: null,
      messageForm: {
        content: ''
      },
      messageRules: {
        content: [
          { required: true, message: '请输入私信内容', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    searchType() {
      this.currentPage = 1
      this.loadData()
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    handleSearch() {
      this.currentPage = 1
      this.loadData()
    },
    handlePageChange(page) {
      this.loadData()
    },
    async loadData() {
      try {
        this.loading = true
        const params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          queryType: this.searchType,
          name: this.searchType === 5 ? this.searchKey : undefined
        }
        const res = await getAlumniList(params)
        
        if (res.success) {
          const { records, total } = res.data
          this.alumniList = records
          this.total = total
        }
      } catch (error) {
        this.$message.error('加载失败')
      } finally {
        this.loading = false
      }
    },
    goToDetail(item) {
      console.log(item)
      this.$router.push({
        path: '/graduate/details',
        query: { id: item.id }
      })
    },
    // 处理发送私信按钮点击
    handleSendMessage(alumni) {
      this.currentAlumni = alumni;
      this.messageDialogVisible = true;
      this.messageForm.content = '';
    },
    
    // 发送私信
    async sendMessage() {
      if (!this.currentAlumni) return;
      
      try {
        await this.$refs.messageForm.validate();
        
        this.sending = true;
        const res = await sendMessage({
          recipientId: this.currentAlumni.id,
          content: this.messageForm.content
        });

        if (res.code === 200) {
          this.$message.success('发送成功');
          this.messageDialogVisible = false;
        } else {
          this.$message.error(res.message || '发送失败');
        }
      } catch (error) {
        console.error('发送私信失败:', error);
        this.$message.error('发送失败，请重试');
      } finally {
        this.sending = false;
      }
    },
    getAvatarSrc(profilePicture) {
      if (!profilePicture) {
        return require('@/assets/img/profile/profile-default.png');
      }
      
      // 处理接口返回的路径
      const match = profilePicture.match(/profile-(man|women)-\d\.png$/);
      if (match) {
        // 如果是预设头像，从本地加载
        try {
          return require(`@/assets/img/profile/${profilePicture.split('/').pop()}`);
        } catch (e) {
          return require('@/assets/img/profile/profile-default.png');
        }
      }
      
      // 如果是其他格式的头像，直接返回
      return profilePicture;
    }
  }
}
</script>

<style lang="less" scoped>
.graduate {
  min-height: 100vh;
  background: linear-gradient(180deg, #f0f7ff 0%, #ffffff 100%);

  .search-header {
    position: sticky;
    top: 0;
    z-index: 99;
    background: #fff;
    padding: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

    .type-selector {
      display: flex;
      justify-content: space-between;
      padding: 8px 0;
      margin-bottom: 12px;
      
      .type-item {
        flex: 1;
        height: 40px;
        line-height: 40px;
        text-align: center;
        margin: 0 6px;
        background: #f5f7fa;
        color: #666;
        font-size: 16px;
        border-radius: 20px;
        transition: all 0.3s ease;
        position: relative;
        cursor: pointer;
        
        &.active {
          background: #4790ff;
          color: #fff;
          font-weight: 500;
          box-shadow: 0 2px 8px rgba(71, 144, 255, 0.3);
          
          &::after {
            content: '';
            position: absolute;
            bottom: -6px;
            left: 50%;
            transform: translateX(-50%);
            border: 6px solid transparent;
            border-top-color: #4790ff;
          }
        }
        
        &:hover:not(.active) {
          background: #e8f0fe;
          color: #4790ff;
        }
      }
    }

    .search-box {
      margin-top: 16px;
      padding: 0 12px;
      
      .search-input {
        :deep(.el-input__inner) {
          height: 40px;
          line-height: 40px;
          border-radius: 20px 0 0 20px;
          border-right: none;
          background: #f5f7fa;
          border-color: #f5f7fa;
          
          &:focus {
            border-color: #4790ff;
            
            & + .el-input-group__append {
              border-color: #4790ff;
            }
          }
        }
        
        :deep(.el-input-group__append) {
          background: #4790ff;
          border-color: #f5f7fa;
          border-radius: 0 20px 20px 0;
          padding: 0 20px;
          
          .el-button {
            border: none;
            background: transparent;
            color: #fff;
            padding: 0;
            
            &:hover {
              color: #fff;
              opacity: 0.9;
            }
          }
        }
      }
    }
  }

  .alumni-list {
    padding: 16px;
    position: relative;
    min-height: 200px;

    .alumni-item {
      margin-bottom: 12px;
      border-radius: 8px;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .item-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 8px;
        
        .main-info {
          display: flex;
          align-items: center;
          flex: 1;
          cursor: pointer;
          margin-right: 16px;
          
          &:hover {
            opacity: 0.8;
          }
        }
        
        .message-btn {
          // 修改按钮样式
          background: #fff;
          border-color: #4790ff;
          color: #4790ff;
          width: 32px;
          height: 32px;
          padding: 0;
          transition: all 0.3s ease;
          
          &:hover, &:focus {
            background: #4790ff;
            color: #fff;
            transform: scale(1.05);
          }
          
          &:active {
            transform: scale(0.95);
          }
          
          i {
            margin: 0;
            font-size: 16px;
          }
        }
      }
    }

    .load-more {
      text-align: center;
      margin-top: 20px;
      padding: 10px 0;
    }

    .loading-state {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

// 分页器样式优化
:deep(.el-pagination.is-background) {
  .el-pager li:not(.disabled).active {
    background-color: #4790ff;
  }
  .el-pager li:not(.disabled):hover {
    color: #4790ff;
  }
}

.message-dialog {
  /deep/ .el-dialog__body {
    padding: 20px;
  }
  
  /deep/ .el-textarea__inner {
    background: #f7fafd;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
    padding: 12px;
    
    &:focus {
      border-color: #4790ff;
    }
  }
  
  @media screen and (max-width: 768px) {
    /deep/ .el-dialog {
      width: 90% !important;
      margin-top: 20vh !important;
    }
  }
}

:deep(.el-avatar) {
  background-color: #FFE4E1 !important;
}
</style>

<style>
.graduate .van-tabs__line {
  background-color: #4790ff;
}

.graduate .van-tab--active {
  color: #4790ff;
  font-weight: bold;
}

.graduate .van-search__content {
  background: #f5f5f5;
}
</style>

